package cn.java.homework03;

import java.io.*;
import java.net.*;

public class ServerDemo {
    public static void main(String[] args) {
        ServerSocket ss = null;
        Socket s = null;
        BufferedReader br = null;
        PrintWriter pw = null;
        ObjectInputStream ois = null;
        try {
            ss = new ServerSocket(8888);
            System.out.println("服务器已经启动，正在监听....");
            s = ss.accept();
            System.out.println("服务器连接成功....");
            br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            pw = new PrintWriter(s.getOutputStream(), true);
            ois = new ObjectInputStream(s.getInputStream());
            pw.println("请输入账号：");
            String len1 = br.readLine();
            pw.println("请输入密码");
            String len2 = br.readLine();
            if (len1.equals("bjsxt") && len2.equals("bjsxt")) {
                pw.println("登陆成功");
                User user =(User) ois.readObject();
                System.out.println(user.toString());
            } else {
                pw.println("登陆失败，账号或密码错误！");
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                assert pw != null;
                pw.close();
                br.close();
                s.close();
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
